package cn.yunhe.controller;

import cn.yunhe.entity.Field;
import cn.yunhe.service.impl.FieldServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FieldController {

    @Resource
    private FieldServiceImpl fieldService;


    @RequestMapping("/page")
    public Map queryFieldByPage(int pageSize, int pageNum, Field field) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("field", field);
        Page page = fieldService.getFieldListByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;
    }

    @RequestMapping("/addField")
    public int addField(Field field) {
        return fieldService.addField(field);
    }

    @RequestMapping("/delField")
    public int delField(Field field) {
        return fieldService.delField(field);
    }

    @RequestMapping("getFieldById")
    public ModelAndView getFieldById(Field field, HttpServletRequest request) {
        Field f = fieldService.getFieldById(field);
        request.setAttribute("data", f);
        return new ModelAndView("updField");
    }

    @RequestMapping("/upField")
    public int updField(Field field) {
        return fieldService.updField(field);
    }

    @RequestMapping("getListField")
    public List<Field> getListField() {
        return fieldService.getListField();
    }


    /**
     * 获取所有分类
     *
     * @return 郭瀚钰
     */
    @RequestMapping("/getAllField")
    public List<Field> getAllField() {

        List<Field> fieldList = fieldService.getAllField();

        return fieldList;
    }


    public FieldServiceImpl getFieldService() {
        return fieldService;
    }

    public void setFieldService(FieldServiceImpl fieldService) {
        this.fieldService = fieldService;
    }
}
